#ifndef _GAME_RMS_H_
#define _GAME_RMS_H_

#include <vector>
#include "util/file_io.h"
#include "../sleipner_config.h"
namespace LuaPlus
{
	class LuaState;
}
#ifdef _PLATFORM_WIN32
#pragma pack(1)
#endif
/***
 * @brief:	Function to handle persistant user-states, high-scores, profiles, etc
 */
class GameRMS
{	
public:
	static const int	LEVEL_C = 2000;
	static const int	CONST_C = 3000;
	static const int	TROPHY_C = 1000;
	static const int	MAX_PROFILES = 7;
	static const int	NAME_LENGTH = 250;

	struct	Level
	{
		int				State;
		int				Score;
		int				HighestRank;
	};

	struct	Profile
	{
		char			Name[NAME_LENGTH];
		char			Language[NAME_LENGTH];
		char			Location[NAME_LENGTH];
		Level			LevelA[LEVEL_C];
		int				ConstantA[CONST_C];
		int				TrophyA[TROPHY_C];
		bool			Cheating;
	};
	struct	Header
	{
		unsigned int	MagicNum;
		unsigned int	VersionNum;
		int				UserID;

		bool			UseFullscreen;
		bool			HintOn;
		bool			CanTilt;
		bool			TiltOn;
		bool			CanVibrate;
		bool			VibrateOn;
		float			SoundVolume;
		float			MusicVolume;	
		char			Language[NAME_LENGTH];

		int				ProfileC;
		int				CurrentProfile;
		Profile			Profiles[MAX_PROFILES];
	};	

	GameRMS();
	~GameRMS();
	static GameRMS*		Instance();
	void				Load();
	void				Save();
	void				Reset();
	void				Register(LuaPlus::LuaState* _pScript);

	bool				IsUseFullscreenEnabled() { return m_Data.UseFullscreen; };
	void				SetUseFullscreenEnabled(bool b) { m_Data.UseFullscreen = b; }; 
	float				GetSoundVolume() { return m_Data.SoundVolume; };
	float				GetMusicVolume() { return m_Data.MusicVolume; };
	void				SetSoundVolume(float f) { m_Data.SoundVolume = f; };
	void				SetMusicVolume(float f) { m_Data.MusicVolume = f; };
	void				SetEnableHint(bool b)	{ m_Data.HintOn = b; };
	void				SetEnableVibrate(bool b) { m_Data.VibrateOn = b; };
	void				SetEnableTilt(bool b)	{ m_Data.TiltOn = b; };
	void				SetCanTilt(bool b) { m_Data.CanTilt = b; };
	void				SetCanVibrate(bool b) { m_Data.CanVibrate = b; };
	bool				IsHintEnabled() const { return m_Data.HintOn; };
	bool				IsTiltEnabled() const { return m_Data.TiltOn; };
	bool				IsVibrateEnabled() const { return m_Data.VibrateOn; };
	bool				GetCanTilt() const { return m_Data.CanTilt; };
	bool				GetCanVibrate() const { return m_Data.CanVibrate; };
	bool				IsLoaded() const { return m_IsLoaded; };

// profiles
	virtual bool		AddProfile(const std::string& Name, const std::string& Language, const std::string& Location);
	int					GetProfileC() { return m_Data.ProfileC; };
	int					GetMaxProfileC() { return MAX_PROFILES; };
	int					GetCurrentProfile() { return m_Data.CurrentProfile; };
	bool				ProfileValid() { return m_Data.CurrentProfile >= 0 && m_Data.CurrentProfile<m_Data.ProfileC; };
	void				SetProfile(int ID);
	void				DeleteProfile(int ID);
	const char*			GetName();
	const char*			GetNameFromID(int ID);
	const char*			GetLanguage();
	void				SetLanguage(const std::string& Name);
	const char*			GetLanguageFromID(int ID);
	const char*			GetLocation();
	const char*			GetLocationFromID(int ID);
	Profile*			GetProfile();
	virtual bool		IsValidProfileNameA(const std::string& Name);
	virtual bool		IsValidProfileNameW(const std::wstring& Name);
	bool				ProfileNameExistsA(const std::string& Name, bool CaseSensitive = false);
	bool				ProfileNameExistsW(const std::wstring& Name, bool CaseSensitive = false);
	
// level stuff
	bool				IsCheating();
	int					IsOpen(int Index);
	bool				IsCleared(int Index);
	void				SetOpen(int Index, int Val);
	void				SetInt(int Index, int Value);
	int					GetInt(int Index);
	int					GetRank(int Index);
	int					GetScore(int Index);
	void				SetRank(int Index, int Rank);
	void				SetScore(int Index, int Score);

// trophy stuff
	int					GetTrophyValue(int Index);
	void				SetTrophyValue(int Index, int Value);

	virtual void				ClearCustomProfile(int ID) {};
	virtual void				LoadCustom(util::FileIO* pFile) {};
	virtual void				SaveCustom(util::FileIO* pFile) {};
	virtual void				DeleteCustomProfile(int ID) {};
	virtual void				ResetCustom() { };
	virtual void				RegisterCustom(LuaPlus::LuaState* _pScript) {};
	virtual unsigned long		GetMagic() { return 'dflt'; };
	virtual unsigned long		GetVersion() { return 0x30; };

protected:
	// defaults
	virtual float				DefaultMusicVol() const { return 100.0f; }
	virtual float				DefaultSoundVol() const { return 100.0f; }

private:
	Header				m_Data;
	std::string			m_Filename;
	bool				m_IsLoaded;
};

#ifdef _PLATFORM_WIN32
#pragma pack()
#endif
#endif // _GAME_RMS_H_